// pages/map/map.js
const app = getApp();
const cloudUrl = app.globalData.cloudUrl;
let i = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    app:app,
    longitude:116.46,
    latitude:39.92,
    showLoc:true,
    animation: {},
    searchIcon:cloudUrl+"cearchIcon.png",
    roadIcon:cloudUrl+"map/road.png",
    scenicIcon:cloudUrl+"map/scenic.png",
    locationIcon:cloudUrl+"map/location.png",
    closeIcon:"https://6275-buxucloud-01-7gvvu046d141963a-1324829580.tcb.qcloud.la/closeIcon.png?sign=a3e243ccbeb66022f41c71e083cb4d42&t=1710480340",
    showPop:false
  },

  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap');
    wx.setNavigationBarTitle({
      title: '景区导航',
    })
  },
  onLoad(options) {
    this.animation = wx.createAnimation();

    try{
      var point = JSON.parse(options.data);
      if(point && point.length > 0){
        this.setData({
          longitude:point[0],
          latitude:point[1]
        })
      }
    }
    catch(e){
      console.log("-------->地图报错");
      console.log(e);
    }
  },
  onClearClick(){
    this.setData({showPop:!this.data.showPop});
  },
  onImageTap: function () {
    i ++;
    console.log(i)
    this.animation.rotate(360 * i).step();
    this.setData({
        animation: this.animation.export(),
    });
    this.getCenterLocation();
},
  getCenterLocation: function () {
    // this.mapCtx.getCenterLocation({
    //   success: function(res){
    //     console.log(res.longitude)
    //     console.log(res.latitude)
    //   }
    // })
    var _this = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
        _this.setData({
          longitude:res.longitude,
          latitude:res.latitude
        })
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 0,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})